package org.sanm.domain.amap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sanm.common.Result;
import org.sanm.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/amap")
@Tag(name = "高德Web服务API", description = "高德Web服务API")
public class AmapController {
    @Value("${amap.key}")
    private String key;
    @Value("${amap.geo-url}")
    private String geoUrl;

    @GetMapping("geo")
    @Operation(summary = "根据关键字查询坐标", description = "根据关键字查询坐标")
    public Result geo(@RequestParam("keyword") String keyword) throws JsonProcessingException {
        String res = HttpUtil.doGet(geoUrl, Map.of("key", key, "address", keyword));
        ObjectMapper mapper = new ObjectMapper();
        Map ret = mapper.readValue(res, Map.class);
        return Result.success(ret);
    }
}
